/*****************************************************************************//*!
* file  : flash 服务程序
* creat : apleilx
* data  : 2017/11/05
*******************************************************************************/
#define  _FLASH_TASK_MODULE_
#include "os_obj.h"
#include "sys_task.h"
#include "pincfg.h"
#include "sys_init.h"
#include "com_obj.h"
#include "common.h"
#include "bsp_hal.h"
#include "flash_cfg.h"
#include "flash_task.h"
#include "gd25q_spi.h"
#include "gd25q_cmd.h"
#include "str_ext.h"

void  flash_check(ULONG thread_input);
void flash_usb_handle(uint32_t cmd);
void flash_time_hist_read(void);

/*****************************************************************************
* @brief   system task.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void  flash_task(ULONG thread_input)
{
	(void)thread_input;

	while (!os_obj.qid_flash)
	{
		osDelay(10);
	}

	gd25_spi_init();
	gd25_init(0);

	gd25_id_read(flash_opt.flash_id);

	hist_log_cfg.api->init();
	hist_dat_cfg.api->init();
    
    flash_time_hist_read();
    
	while (1)
	{   
        flash_opt.cmd_get[0] = 0;
        
		tx_queue_receive(os_obj.qid_flash, flash_opt.cmd_get, ms_ticks(10000));
        
        switch(flash_opt.cmd_get[0])
        {
            //clear
            case (FLASH_OPT_CLR) :
            {
                sys_st.led_req.flash_clr_st = 1;
                
                gd25_chip_erase();
                
                while(! gd25_wait_for_busy()){};
                
				hist_log_cfg.api->init();
				hist_dat_cfg.api->init();

                sys_st.led_req.flash_clr_st = 0;                
            }
            
            //log save
            case ( FLASH_OPT_RS_LOG) :
            {
                flash_opt.hist_rslog[1] = hist_dat_cfg.info->site_next;
                
				hist_log_cfg.api->save();
            }
            
            //dada save1
            case ( FLASH_OPT_HEAD ) :
            {
                copy_u8(&sys_st.hist_head, flash_opt.hist_dat, 16);
				hist_dat_cfg.api->save();
            }

            //dada save2
            case (FLASH_OPT_MID) :
            {
                copy_u8(&sys_st.hist_mid, flash_opt.hist_dat, 16);
                hist_dat_cfg.api->save();
            }
            
            //dada save3
            case  ( FLASH_OPT_TAIL) :
            {
                copy_u8(&sys_st.hist_tail, flash_opt.hist_dat, 16);
                hist_dat_cfg.api->save();
            }
            //usb cmd
            default:
            {
                flash_usb_handle(flash_opt.cmd_get[0]);
            }
        }
	}
}

/*******************************************************************************
* @brief  flash_usb_handle
* \param[in] none
* \retval: none
*******************************************************************************/
void flash_usb_handle(uint32_t cmd)
{
	switch (cmd)
	{
		case USB_CMD_READ:
		{
			// 指定位置读取数据
			gd25_read(flash_opt.usb_cmd.read_addr, flash_opt.usb_cmd.read_len, flash_opt.usb_cmd.read_pdat);
            flash_opt.usb_cmd.req = USB_CMD_NONE;
            
			break;
		}
		case USB_CMD_SEARCH_WITH_RS_AMOUNT:
		{
            int32_t site;
            uint32_t log_read[4];
			uint32_t hist_read[8+2];
            
			// 搜索指定次数
            site = hist_log_cfg.api->site_fore(hist_log_cfg.info->site_next, flash_opt.usb_cmd.search_rs_amount);
			
			// 获取日志
			hist_log_cfg.api->read(site, log_read);
            
            
			// 获取最早历史数据
			hist_dat_cfg.api->read(hist_dat_cfg.info->site_start, hist_read);
			
			// 判定次数有效性
			if(log_read[0] > hist_read[0])
			{
				// 有效
				flash_opt.usb_cmd.search_time.val = log_read[0];
				flash_opt.usb_cmd.search_site = log_read[1];
				flash_opt.usb_cmd.search_addr = flash_opt.usb_cmd.search_site * hist_dat_cfg.dat_size;
			}
			else
			{
				// 无效
				flash_opt.usb_cmd.search_time.val = hist_read[0];
				flash_opt.usb_cmd.search_site = hist_dat_cfg.info->site_start;
				flash_opt.usb_cmd.search_addr = flash_opt.usb_cmd.search_site * hist_dat_cfg.dat_size;
			}
            
            flash_opt.usb_cmd.req = USB_CMD_NONE;
			break;
		}
		case USB_CMD_SEARCH_WITH_TIME:
		{
            int32_t site;
            
			// 搜索指定日期的数据
			site = hist_dat_cfg.api->quick_search(flash_opt.usb_cmd.search_time, flash_opt.usb_cmd.search_mode);
            flash_opt.usb_cmd.search_site = site;
            flash_opt.usb_cmd.search_addr = site * hist_dat_cfg.dat_size;
            
            flash_opt.usb_cmd.req = USB_CMD_NONE;
			break;
		}
		default:
		{
			break;
		}
	}
}

/*******************************************************************************
* @brief  flash_time_hist_read
* \param[in] none
* \retval: none
*******************************************************************************/
void flash_time_hist_read(void)
{
    int32_t site;
    
    site = simp_log_site_fore(&hist_dat_cfg, hist_dat_cfg.info->site_next, 1);
    if(site < 0)
        site = 0;
    
    simp_log_read(&hist_dat_cfg, site, &flash_opt.hist_dat);
    
    sys_st.hist_opt.time_hist_powerup.val = flash_opt.hist_dat[0];
    
    if(sys_st.hist_opt.time_hist_powerup.time.mon > 12)
    {
        sys_st.hist_opt.time_hist_powerup.time.year = 20;
        sys_st.hist_opt.time_hist_powerup.time.mon = 6;
        sys_st.hist_opt.time_hist_powerup.time.day = 15;
        sys_st.hist_opt.time_hist_powerup.time.hour = 12;
        sys_st.hist_opt.time_hist_powerup.time.min = 30;
        sys_st.hist_opt.time_hist_powerup.time.sec2t = 0;
    }
}
